package com.ipay.exception;

import com.ipay.result.ErrorMsg;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：前世小和尚（xiaopingzhang8@creditease.cn）
 * @Description ：全局异常处理
 * @ClassName ：GlobalExceptionHandler
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2018/4/8 14:33
 */
@ControllerAdvice(basePackages = "com.ipay.controller")
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("exception", e);
        mav.addObject("reqURL", req.getRequestURL());
        return mav;
    }

    @ExceptionHandler(value = IpayException.class)
    @ResponseBody
    public ErrorMsg<String> customErrorHandler(HttpServletRequest req, IpayException e) {
        ErrorMsg<String> msg = new ErrorMsg<String>();
        msg.setCode(ErrorMsg.ResultCode.ERROR.getCode());
        msg.setDesc(ErrorMsg.ResultCode.ERROR.getDesc());
        msg.setData(e.getMessage());
        msg.setUrl(req.getRequestURL().toString());
        return msg;
    }

}
